<?php

namespace app\admin\model\shopro\cats;

use app\admin\model\shopro\Category;
use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'shopro_cat_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'dispatch_type_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    //发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货
    public function getDispatchTypeList()
    {
        return ['express' => __('物流快递'), 'selfetch' => __('用户自提'), 'store' => __('商家配送'), 'autosend' => __('自动发货')];
    }

    public function getStatusList()
    {
        return ['0' => __('隐藏'), '1' => __('正常')];
    }


    public function getDispatchTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['dispatch_type']) ? $data['dispatch_type'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getDispatchTypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setDispatchTypeAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function goodsCategory(){
        return $this->hasOne(Category::class,"id","category_ids")->setEagerlyType(0);
    }

}
